package com.example.myapplication;

import android.content.ClipData;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.icu.text.SimpleDateFormat;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.db.domain.AccountBean;
import com.example.myapplication.db.DBManager;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class addFragment extends AppCompatActivity {

    // true 代表支出
    private boolean status=true;

    //  取消
    private TextView add_cancel;
    //  支出
    private TextView add_nav_out;
    //  支出下划线
    private View out_view;
    //  收入
    private TextView add_nav_in;
    //  收入下划线
    private View in_view;
    //  自定义的键盘
    private Keyboard keyboard;
    //
    private KeyboardView keyboardView;
    //  显示金额
    private EditText money;
    //  备注
    private EditText remark;
    //  时间
    private TextView now_time;
    //  拍照
    private ImageView camera;
    //  导入图片
    private ImageView import_image;
    // 视图
    private GridView gridView;
    // 数据
    private List<Map<String,Object>> data_list;
    // 适配器
    private SimpleAdapter adapter;
    // 图片数据
    private final int[] icon = {R.drawable.meal,R.drawable.shop,R.drawable.daily,R.drawable.transport,R.drawable.fruit,
            R.drawable.snakes,R.drawable.sport,R.drawable.entertainment,R.drawable.phone,R.drawable.cloth,R.drawable.beauty,
            R.drawable.social,R.drawable.travel,R.drawable.digit,R.drawable.hospital,R.drawable.study,R.drawable.dog,
            R.drawable.redbag,R.drawable.gift,R.drawable.book,R.drawable.fix,R.drawable.donate,R.drawable.icon_package,
    };
    // 名字数据
    private final String[] iconName = {"餐饮","购物","日用","交通","水果","零食","运动","娱乐","通讯","服饰","美容"
            ,"社交","旅行","数码","医疗","学习","宠物","礼金","礼物","书籍","维修","捐赠","快递"
    };

    //  收入数据
    private List<Map<String,Object>> in_data_list;
    // 图片数据
    private final int[] in_icon = {R.drawable.wages,R.drawable.parttime,R.drawable.money_management,R.drawable.redbag,
            R.drawable.more
    };
    // 名字数据
    private final String[] in_iconName = {"工资","兼职","理财","礼金","其他"
    };
    // 选中类型
    private int checkIndex = 0;
    //存储拍照所得图片路径
    private Uri mImageUri;
    //拍照请求码
    private final static int ASK_FOR_CAMERA = 1;
    //存储拍照增强后结果
    private Bitmap mBitmap = null;
    //存储用户选择的图片路径
    private ArrayList<Uri> uriArrayList = new ArrayList<>();
    //接收回调结果
    private ActivityResultLauncher<Intent> mResultLauncher;
    //存储读取相册增强后结果
    private ArrayList<Bitmap> bitmapArrayList = new ArrayList<>();

    //记录调用接口次数
    private int invokeCount = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_fragment);

        //网络问题的简单解决
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        keyboardView = findViewById(R.id.keyboard_record);
        money = findViewById(R.id.keyboard_money);
        remark = findViewById(R.id.keyboard_remark);
        now_time = findViewById(R.id.now_time_ymd);
        camera = findViewById(R.id.camera);
        import_image = findViewById(R.id.add_import_image);
        add_cancel = findViewById(R.id.add_cancel);
        gridView = findViewById(R.id.replaceable_view);
        add_nav_out = findViewById(R.id.add_nav_out);
        add_nav_in = findViewById(R.id.add_nav_in);
        out_view = findViewById(R.id.out_view);
        in_view = findViewById(R.id.in_view);

        // 获取当前时间
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat now  = new SimpleDateFormat("yyyy-MM-dd");
        now_time.setText(now.format(date));

        keyutils myKeyboard = new keyutils(keyboard,keyboardView,money);
        myKeyboard.showKey();

        // 初始化
        init();


        // 设置事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                checkIndex = i;
            }
        });

        // 收入支出点击事件
        add_nav_out.setOnClickListener(view -> {
            // 显示支出界面
            status = true;
            init();
            out_view.setVisibility(View.VISIBLE);
            in_view.setVisibility(View.INVISIBLE);
        });

        add_nav_in.setOnClickListener(view -> {
            // 显示收入界面
            status = false;
            show();
            in_view.setVisibility(View.VISIBLE);
            out_view.setVisibility(View.INVISIBLE);
        });

        // 设置保存后调用方法
        myKeyboard.setOnEnsureListener(new keyutils.OnEnsureListener() {
            @Override
            public void onEnsure() {
                //  返回清单页面
                addFragment.this.finish();
                //TODO: 保存信息
                AccountBean accountBean = new AccountBean();
                accountBean.setRemark(remark.getText().toString());
                //暂未获取user_id
                accountBean.setUser_id(1);
                //TODO:用户输入不正确时提示？
                accountBean.setMoney(Float.parseFloat(money.getText().toString()));
                //获取收支类型
                if(status){//status为true代表支出
                    accountBean.setType(iconName[checkIndex]);
                    accountBean.setIn_out("支出");
                }else {
                    accountBean.setType(in_iconName[checkIndex]);
                    accountBean.setIn_out("收入");
                }
                //获取选择的时间
                String dateTime = String.valueOf(now_time.getText());
                String yearStr = dateTime.split("-")[0];
                String monthStr = dateTime.split("-")[1];
                String dayStr = dateTime.split("-")[2];

                int year = Integer.parseInt(yearStr);
                int month = Integer.parseInt(monthStr);
                int day = Integer.parseInt(dayStr);

                accountBean.setYear(year);
                accountBean.setMonth(month);
                accountBean.setDay(day);

                DBManager.insertAccount(accountBean);

            }
        });


        add_cancel.setOnClickListener(view -> addFragment.this.finish());

        // 调出日历
        now_time.setOnClickListener(view -> {
            String dateTime = String.valueOf(now_time.getText());
            String yearStr = dateTime.split("-")[0];
            String monthStr = dateTime.split("-")[1];
            String dayStr = dateTime.split("-")[2];

            int year = Integer.parseInt(yearStr);
            int month = Integer.parseInt(monthStr)-1;
            int day = Integer.parseInt(dayStr);

            // 日历
            MyCalendar.getCalendar(addFragment.this,now_time,year,month,day);
        });

        //调用相机
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                askForCamera();
            }
        });
        // 导入图片
        import_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO:导入图片页面
                askForAlbum();
            }
        });

        //读取相册回调
        mResultLauncher =  registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if(result.getResultCode() == RESULT_OK){

                            Intent intent =result.getData();
                            //单张图片用单独Uri存储
                            Uri uri=intent.getData();

                            ClipData imageNames = result.getData().getClipData();
                            System.out.println(imageNames);
                            if(imageNames != null){
                                for(int i=0;i<imageNames.getItemCount();i++){
                                    Uri imageUri = imageNames.getItemAt(i).getUri();
                                    uriArrayList.add(imageUri);
                                }
                                System.out.println(uriArrayList.size());
                            }else {
                                uriArrayList.add(uri);
                            }
                            //获得用户选择的图片路径后将每张图片转为二进制流并调用图像增强接口
                            for(int i=0;i<uriArrayList.size();i++){
                                try {
                                    byte[] imgData = readStream(getContentResolver().openInputStream(Uri.parse(uriArrayList.get(i).toString())));
                                    imageEnhance(imgData,2);
                                    imageRecognition(bitmapArrayList.get(i));
                                } catch (Exception e) {
                                    throw new RuntimeException(e);
                                }
                            }
//                            for(int i=0;i<bitmapArrayList.size();i++){
//                                try {
//                                    imageRecognition(bitmapArrayList.get(i));
//                                } catch (Exception e) {
//                                    throw new RuntimeException(e);
//                                }
//                            }
                            System.out.println("Total invoke counts:"+invokeCount);
                        }
                    }
                }
        );


    }

    private void show(){
        // 获取数据
        in_data_list = new ArrayList<>();
        getData(in_icon,in_iconName,in_data_list);
        // 新建适配器
        String [] from = {"image","text"};
        int [] to = {R.id.image,R.id.text};
        adapter = new SimpleAdapter(addFragment.this,in_data_list,R.layout.item,from,to);

        // 配置适配器
        gridView.setAdapter(adapter);
        gridView.setSelector(R.drawable.item_pressed);
    }

    private void init(){
        // 获取数据
        data_list = new ArrayList<>();
        getData(icon,iconName,data_list);
        // 新建适配器
        String [] from = {"image","text"};
        int [] to = {R.id.image,R.id.text};
        adapter = new SimpleAdapter(addFragment.this,data_list,R.layout.item,from,to);

        // 配置适配器
        gridView.setAdapter(adapter);
        gridView.setSelector(R.drawable.item_pressed);
//        默认选中第一个

        gridView.setItemChecked(0,true);
    }
    private void getData(int[] icon,String[] iconName,List<Map<String,Object>> data_list) {
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }
    }

    //唤起相机
    private void askForCamera(){
        //清空ArrayList
        uriArrayList.clear();
        bitmapArrayList.clear();
        mBitmap = null;

        File outputImage=new File(getExternalCacheDir(),"output_image.jpg");
        try//判断图片是否存在，存在则删除在创建，不存在则直接创建
        {
            if(outputImage.exists())
            {
                outputImage.delete();
            }
            outputImage.createNewFile();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if(Build.VERSION.SDK_INT>=24)
            //判断安卓的版本是否高于7.0，高于则调用高于的方法，低于则调用低于的方法
            //把文件转换成Uri对象
                    /*
                    之所以这样，是因为android7.0以后直接使用本地真实路径是不安全的，会抛出异常。
                    FileProvider是一种特殊的内容提供器，可以对数据进行保护
                     */
        {
            mImageUri= FileProvider.getUriForFile(addFragment.this,
                    "com.example.cameraalbumtest.fileprovider",outputImage);
        }
        else {
            mImageUri=Uri.fromFile(outputImage);
        }
        //使用隐示的Intent，系统会找到与它对应的活动，即调用摄像头，并把它存储
        Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,mImageUri);
        startActivityForResult(intent,ASK_FOR_CAMERA);
        //调用会返回结果的开启方式，返回成功的话，则把它显示出来
    }

    //处理调用相机的回调
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ASK_FOR_CAMERA:
                if (resultCode == RESULT_OK) {
                    try {
                        byte[] imgData = readStream(getContentResolver().openInputStream(Uri.parse(mImageUri.toString())));
                        imageEnhance(imgData,1);
                        //图片识别
                        imageRecognition(mBitmap);

                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                break;
            default:
                break;
        }
    }

    //唤起系统相册，单击单选，长按多选
    private void askForAlbum(){
        //清空ArrayList
        uriArrayList.clear();
        bitmapArrayList.clear();
        System.out.println(uriArrayList.size());

        //跳转到系统相册，选择图片并返回
        Intent intent = new Intent();
        //设置内容为图片类型
        intent.setType("image/*");
        //单击选择图片，长按多选图片
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //打开系统相册，等待图片选择结果
        mResultLauncher.launch(intent);
    }

    //调用图像增强接口
    public void imageEnhance(byte[] imgData,int type){
        // 图片切边增强
        String url = "https://api.textin.com/ai/service/v1/crop_enhance_image";
        // 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id
        // 示例代码中 x-ti-app-id 非真实数据
        String appId = "1cb72742a008938bfdb0bbd22cb8962e";
        // 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code
        // 示例代码中 x-ti-secret-code 非真实数据
        String secretCode = "16d5fe231f27cd159ace3bfa6d87c121";
        BufferedReader in = null;
        DataOutputStream out = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("x-ti-app-id", appId);
            conn.setRequestProperty("x-ti-secret-code", secretCode);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // 设置请求方式
            out = new DataOutputStream(conn.getOutputStream());
            out.write(imgData);
            out.flush();
            out.close();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            invokeCount++;
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        //读取json串
        JSONObject jsonObject =JSON.parseObject(result);
        String test = JSONObject.parseObject(jsonObject.getString("result")).getString("image_list");
        String[] outcome = test.split(",");

        //去除前缀与后引号，保留字符串部分,结果即为图片base64字符串
        String substring = outcome[0].substring(9,outcome[0].length()-1);
        //base64转换为二进制
        byte[] b = new byte[0];
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            b = Base64.getMimeDecoder().decode(substring);
        }

        //拍照结果直接存储在mBitmap中，读取相册结果存储在bitmapArrayList中
        if(type == 1){
            mBitmap = getBitmapFromByte(b);
        }else {
            bitmapArrayList.add(getBitmapFromByte(b));
        }

    }

    //uri转二进制流
    public static byte[] readStream(InputStream inStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }

    //二进制流转bitmap
    public Bitmap getBitmapFromByte(byte[] temp){
        if(temp != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
            return bitmap;
        }else{
            return null;
        }
    }

    public  void imageRecognition(Bitmap mBitmap) throws Exception {
        // 通用文字识别
        String url = "https://api.textin.com/ai/service/v2/recognize";
        // 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id
        // 示例代码中 x-ti-app-id 非真实数据
        String appId = "2e05784bc8f7f6ab693bcee32dd3012a";
        // 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code
        // 示例代码中 x-ti-secret-code 非真实数据
        String secretCode = "c9d7ce4b5e1a8a4e9af5b89345f090aa";
        BufferedReader in = null;
        DataOutputStream out = null;
        String result = "";
        try {
            byte[] imgData = getBitmapByte(mBitmap); // image
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("x-ti-app-id", appId);
            conn.setRequestProperty("x-ti-secret-code", secretCode);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // 设置请求方式
            out = new DataOutputStream(conn.getOutputStream());
            out.write(imgData);
            out.flush();
            out.close();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if(result.contains("限乘当日当次车")){

            JSONObject jsonObject = new JSONObject().parseObject(result);
            result=jsonObject.getString("result");
            jsonObject = new JSONObject().parseObject(result);
            result=jsonObject.getString("lines");
            String[] str=result.split("\\}\\,");
            for (int i = 0;  i<str.length ; i++) {
                str[i]+="}";

            }

            JSONObject json = new JSONObject().parseObject(str[8]);
            //System.out.println(json.getString("text"));
            JSONObject json2 = new JSONObject().parseObject(str[10]);
            //System.out.println(json2.getString("text"));

            String str1 =json2.getString("text");
            String str2 = "";
            for(int i = 0; i < str1.length(); i++){
                if(str1.charAt(i) >= 48 && str1.charAt(i) <= 57 || str1.charAt(i)==46){
                    str2 += str1.charAt(i);

                }
            }
            //float money=Float.parseFloat(str2);
            money.setText(str2);


            //时间获取(必须采用XXXX年XX月XX日)
            String str4=json.getString("text");
            String str5 =str4.substring(0,4);
            String str6 =str4.substring(5,7);
            String str7 =str4.substring(8,10);
            str5 +="-";
            str5 +=str6;
            str5 +="-";
            str5 +=str7;
            System.out.println(str5);
            now_time.setText(str5);


        }
        else{
            System.out.println(result);
            JSONObject jsonObject = new JSONObject().parseObject(result);
            System.out.println(jsonObject.getString("result"));
            result=jsonObject.getString("result");
            jsonObject = new JSONObject().parseObject(result);
            System.out.println(jsonObject.getString("lines"));
            result=jsonObject.getString("lines");
            result=result.substring(1,result.length()-1);
            System.out.println(result);
            String[] str=result.split("\\}\\,");
            for (int i = 0;  i<str.length-1 ; i++) {
                str[i]+="}";
                System.out.println(str[i]);
            }
            JSONObject[] jsonObjects = new JSONObject[str.length];

        /*
        JSONObject json = new JSONObject().parseObject(str[0]);
        //System.out.println(json.getString("text"));
        JSONObject json1 = new JSONObject().parseObject(str[1]);
        //System.out.println(json1.getString("text"));
        JSONObject json2 = new JSONObject().parseObject(str[2]);
        //System.out.println(json2.getString("text"));
        str[3] = str[3].substring(0,str[3].length()-2);
        JSONObject json3 = new JSONObject().parseObject(str[3]);
        //System.out.println(json3.getString("text"));

         */
            for (int i = 0; i <str.length ; i++) {
                jsonObjects[i] = new JSONObject().parseObject(str[i]);
            }
//                            Intent intent = result.getData();
//                            //单张图片用单独用Uri储存
//                            Uri uri = intent.getData();
//                            System.out.println(uri);
            String str1 =jsonObjects[2].getString("text");
            String str2 = "";
            for(int i = 0; i < str1.length(); i++){
                if(str1.charAt(i) >= 48 && str1.charAt(i) <= 57 || str1.charAt(i)==46){
                    str2 += str1.charAt(i);

                }
            }
            //float money=Float.parseFloat(str2);
            money.setText(str2);


            //时间获取(必须采用XXXX年XX月XX日)
            String str4=jsonObjects[1].getString("text");
            String str5 =str4.substring(3,7);
            String str6 =str4.substring(8,10);
            String str7 =str4.substring(11,13);
            str5 +="-";
            str5 +=str6;
            str5 +="-";
            str5 +=str7;
            //System.out.println(str5);//时间
            now_time.setText(str5);


            String str3=jsonObjects[3].getString("text");
            str3 = str3.substring(3,str3.length());
            for (int i = 4; i <jsonObjects.length ; i++) {
                String str8=jsonObjects[i].getString("text");
                str3 +=str8;
            }
            remark.setText(str3);
            //System.out.println(str3);
        }



        //类型判断、金额、时间、备注
        if(result.contains("饿了么")||result.contains("美团")){
            checkIndex=0;
        }
        if(result.contains("衣")||result.contains("裤")){
            checkIndex=1;
        }if(result.contains("日用")){
            checkIndex=2;
        }if(result.contains("公共")||result.contains("车")){
            checkIndex=3;
        }if(result.contains("水果")){
            checkIndex=4;
        }if(result.contains("零食")||result.contains("小吃")){
            checkIndex=5;
        }if(result.contains("球")||result.contains("运动")){
            checkIndex=6;
        }if(result.contains("唱歌")||result.contains("娱乐")){
            checkIndex=7;
        }if(result.contains("电话费")||result.contains("通讯")){
            checkIndex=8;
        }if(result.contains("服饰")){
            checkIndex=9;
        }if(result.contains("美容")||result.contains("美")){
            checkIndex=10;
        }if(result.contains("社交")){
            checkIndex=11;
        }if(result.contains("车")||result.contains("高铁")||result.contains("飞机")){
            checkIndex=12;
        }if(result.contains("照相机")||result.contains("数码")){
            checkIndex=13;
        }if(result.contains("药")||result.contains("医")){
            checkIndex=14;
        }if(result.contains("书")||result.contains("学习")){
            checkIndex=15;
        }if(result.contains("宠物")){
            checkIndex=16;
        }if(result.contains("礼金")||result.contains("红包")){
            checkIndex=17;
        }if(result.contains("礼物")||result.contains("礼品")){
            checkIndex=18;
        }if(result.contains("书籍")){
            checkIndex=19;
        }if(result.contains("维修")){
            checkIndex=20;
        }if(result.contains("捐赠")){
            checkIndex=21;
        }if(result.contains("快递")){
            checkIndex=22;
        }if(result.contains("工资")){
            checkIndex=0;
        }if(result.contains("兼职")){
            checkIndex=1;
        }if(result.contains("理财")){
            checkIndex=2;
        }

    }


    public static byte[] readfile(String path)
    {
        String imgFile = path;
        InputStream in = null;
        byte[] data = null;
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    public static byte[] getBitmapByte(Bitmap bitmap){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        //参数1转换类型，参数2压缩质量，参数3字节流资源
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }




}